/*
** Created by Bardenet Quentin
*/

#include "Settings.hh"

Settings::Settings(Window &win) : GameScreen(win), _Window(win)
{
    this->_Font = new sf::Font();
    this->_Font->LoadFromFile("starcraft.ttf");

    this->_Content = new RessourceManager();
    this->_Content->AddImage("background.png");
    this->_Background = new Background(this->_Window);
    const sf::Image &_image2 = this->_Content->GetImage("background.png");
    sf::Sprite  *_sprite2 = new sf::Sprite();
    _sprite2->SetImage(_image2);
    this->_Background->Initialize(*_sprite2, *new sf::Vector2f(-0.5, 0));

    sf::String  *Sound = new sf::String("Sound  ON", *this->_Font, 40);
    Sound->SetStyle(sf::String::Underlined);
    Sound->SetColor(sf::Color(200, 128, 0));
    Sound->SetPosition(this->_Window.GetWidth()/6.0, 50.0f);
    this->_Items.push_back(Sound);

    sf::String  *Up = new sf::String("Up       keyUp", *this->_Font, 40);
    Up->SetPosition(this->_Window.GetWidth()/6.0, 100.0f);
    this->_Items.push_back(Up);

    sf::String  *Down = new sf::String("Down   keyDown", *this->_Font, 40);
    Down->SetPosition(this->_Window.GetWidth()/6.0, 150.0f);
    this->_Items.push_back(Down);

    sf::String  *Right = new sf::String("Right  keyRight", *this->_Font, 40);
    Right->SetPosition(this->_Window.GetWidth()/6.0, 200.0f);
    this->_Items.push_back(Right);

    sf::String  *Left = new sf::String("Left   keyLeft", *this->_Font, 40);
    Left->SetPosition(this->_Window.GetWidth()/6.0, 250.0f);
    this->_Items.push_back(Left);

    sf::String  *Fire = new sf::String("Fire   space", *this->_Font, 40);
    Fire->SetPosition(this->_Window.GetWidth()/6.0, 300.0f);
    this->_Items.push_back(Fire);

    sf::String  *Retour = new sf::String("Return", *this->_Font, 40);
    Retour->SetPosition(this->_Window.GetWidth()/6.0, 400.0f);
    this->_Items.push_back(Retour);

    this->_Current = this->_Items.begin();

    this->_ItemSelect.insert(std::pair<std::string, int>("Return", MENU));

    this->_IsWaitingKey = false;

    this->_sound = true;

    this->_KeySets.insert(std::pair<std::string, int>("Up", 293));
    this->_KeySets.insert(std::pair<std::string, int>("Down", 294));
    this->_KeySets.insert(std::pair<std::string, int>("Left", 291));
    this->_KeySets.insert(std::pair<std::string, int>("Right", 292));
    this->_KeySets.insert(std::pair<std::string, int>("Fire", 277));

    this->_KeySelected.insert(std::pair<int, std::string>(97, "a"));
    this->_KeySelected.insert(std::pair<int, std::string>(122, "z"));
    this->_KeySelected.insert(std::pair<int, std::string>(98, "b"));
    this->_KeySelected.insert(std::pair<int, std::string>(99, "c"));
    this->_KeySelected.insert(std::pair<int, std::string>(100, "d"));
    this->_KeySelected.insert(std::pair<int, std::string>(101, "e"));
    this->_KeySelected.insert(std::pair<int, std::string>(102, "f"));
    this->_KeySelected.insert(std::pair<int, std::string>(103, "g"));
    this->_KeySelected.insert(std::pair<int, std::string>(104, "h"));
    this->_KeySelected.insert(std::pair<int, std::string>(105, "i"));
    this->_KeySelected.insert(std::pair<int, std::string>(106, "j"));
    this->_KeySelected.insert(std::pair<int, std::string>(107, "k"));
    this->_KeySelected.insert(std::pair<int, std::string>(108, "l"));
    this->_KeySelected.insert(std::pair<int, std::string>(109, "m"));
    this->_KeySelected.insert(std::pair<int, std::string>(110, "n"));
    this->_KeySelected.insert(std::pair<int, std::string>(111, "o"));
    this->_KeySelected.insert(std::pair<int, std::string>(112, "p"));
    this->_KeySelected.insert(std::pair<int, std::string>(113, "q"));
    this->_KeySelected.insert(std::pair<int, std::string>(114, "r"));
    this->_KeySelected.insert(std::pair<int, std::string>(115, "s"));
    this->_KeySelected.insert(std::pair<int, std::string>(116, "t"));
    this->_KeySelected.insert(std::pair<int, std::string>(117, "u"));
    this->_KeySelected.insert(std::pair<int, std::string>(118, "v"));
    this->_KeySelected.insert(std::pair<int, std::string>(119, "w"));
    this->_KeySelected.insert(std::pair<int, std::string>(120, "x"));
    this->_KeySelected.insert(std::pair<int, std::string>(121, "y"));
    this->_KeySelected.insert(std::pair<int, std::string>(262, "shift"));
    this->_KeySelected.insert(std::pair<int, std::string>(278, "enter"));
    this->_KeySelected.insert(std::pair<int, std::string>(277, "space"));
    this->_KeySelected.insert(std::pair<int, std::string>(291, "keyLeft"));
    this->_KeySelected.insert(std::pair<int, std::string>(292, "keyRight"));
    this->_KeySelected.insert(std::pair<int, std::string>(293, "keyUp"));
    this->_KeySelected.insert(std::pair<int, std::string>(294, "keyDown"));
    this->_KeySelected.insert(std::pair<int, std::string>(279, "backspace"));
    this->_KeySelected.insert(std::pair<int, std::string>(257, "ctrl"));
    this->_KeySelected.insert(std::pair<int, std::string>(259, "alt"));
    this->_KeySelected.insert(std::pair<int, std::string>(48, "0"));
    this->_KeySelected.insert(std::pair<int, std::string>(49, "1"));
    this->_KeySelected.insert(std::pair<int, std::string>(50, "2"));
    this->_KeySelected.insert(std::pair<int, std::string>(51, "3"));
    this->_KeySelected.insert(std::pair<int, std::string>(52, "4"));
    this->_KeySelected.insert(std::pair<int, std::string>(53, "5"));
    this->_KeySelected.insert(std::pair<int, std::string>(54, "6"));
    this->_KeySelected.insert(std::pair<int, std::string>(55, "7"));
    this->_KeySelected.insert(std::pair<int, std::string>(56, "8"));
    this->_KeySelected.insert(std::pair<int, std::string>(57, "9"));
}


Settings::~Settings()
{
}

int     Settings::Run(void)
{
    bool    isRunning = true;
    GameTime    *gameTime = new GameTime();
    Time        timer = 0.0f;
    sf::String  *item;

    while (isRunning)
    {
        timer += gameTime->ElapsedGameTime();
        sf::Event e;
        while (this->_Window.GetEvent(e))
            if ((e.Type == sf::Event::Closed))
                isRunning = false;
            else if ((e.Type == sf::Event::KeyPressed) && _IsWaitingKey == true)
            {
                for (std::map<int, std::string>::iterator it = _KeySelected.begin(); it != _KeySelected.end(); ++it)
                {
                    if ((*it).first == e.Key.Code)
                    {
                        sf::String  *item;

                        item = *this->_Current;
                        std::string t;
                        t = item->GetText();
                        t += (*it).second;
                        item->SetText(t);
                        for (std::map<std::string, int>::iterator it = _KeySets.begin(); it != _KeySets.end(); ++it)
                        {
                            if (t.find((*it).first) != std::string::npos)
                                (*it).second = e.Key.Code;
                        }
                        _param.setKeys(_KeySets);
                    }
                }
                _IsWaitingKey = false;
            }
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Escape))
                isRunning = false;
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Up))
                this->GoPrev();
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Down))
                this->GoNext();
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Return))
                {
                    item = *this->_Current;
                    std::string t = item->GetText();
                    for (std::map<std::string, int>::iterator it = _ItemSelect.begin(); it != _ItemSelect.end(); ++it)
                    {
                        if ((*it).first == t)
                            return (*it).second;
                    }
                    if (t.find("Sound") == std::string::npos)
                    {
                        sf::String  *item;

                        item = *this->_Current;
                        std::string t;
                        t = item->GetText();
                        t.replace(7, t.length(), "");
                        item->SetText(t);
                        _IsWaitingKey = true;
                    }
                    else
                    {
                        if (_sound == true)
                        {
                            sf::String  *item;

                            item = *this->_Current;
                            item->SetText("Sound  OFF");
                            _sound = false;
                            _param.setSoundState(_sound);
                        }
                        else
                        {
                            sf::String  *item;

                            item = *this->_Current;
                            item->SetText("Sound  ON");
                            _sound = true;
                            _param.setSoundState(_sound);
                        }
                    }
                }
        this->Update(gameTime->ElapsedGameTime());
        this->Draw(gameTime->ElapsedGameTime());
    }
    return EXIT;
}

void    Settings::Update(Time ElapsedTime)
{
}

void    Settings::Draw(Time ElapsedTime)
{
    std::list<sf::String*>::iterator it;

    this->_Window.Clear();
    this->_Background->Draw(ElapsedTime);
    for (it = this->_Items.begin(); it != this->_Items.end(); ++it){
        this->_Window.Draw(*(*it));
    }
    this->_Window.Display();
}

void    Settings::GoNext(void)
{
    sf::String  *item;
    std::list<sf::String*>::iterator    tmp;

    tmp = this->_Current;
    ++tmp;
    if (tmp == this->_Items.end())
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        this->_Current = this->_Items.begin();
    }
    else
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        if (this->_Current != this->_Items.end())
            ++this->_Current;
    }

    item = *this->_Current;
    item->SetColor(sf::Color(200, 128, 0));
    item->SetStyle(sf::String::Underlined);
}

void    Settings::GoPrev(void)
{
    sf::String  *item;

    if (this->_Current == this->_Items.begin())
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        this->_Current = this->_Items.end();
        --this->_Current;
    }
    else
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        --this->_Current;
    }
    item = *this->_Current;
    item->SetColor(sf::Color(200, 128, 0));
    item->SetStyle(sf::String::Underlined);
}

int     Settings::Enter(void)
{
    return 0;
}

void    Settings::Parameters(Params p)
{
    _param = p;
}
